import { defineStore } from 'pinia';

interface GlobalState {
  name: string;
  accessToken: string;
  age: number;
  userData: any;
}

export const userUserStore: any = defineStore({
  id: 'user', //id必填，且需要唯一
  state: ():GlobalState => {
    return {
      name: 'yiyi',
      age: 20,
      accessToken: 'xxxxxxxxxxxxx',
      userData: null
    };
  },
  getters: {
    getNewName: (state) => {
      return state.name + '100';
    },
    getAddAge: (state) => {
      return (num: number) => state.age + num;
    },
  },
  actions: {
    updataName(name: string) {
      this.name = name;
    },
    incrementAsync() {
      setTimeout(() => {
        this.age++
      }, 1000)
    },
  },
  //开启数据缓存
  persist: {
    enabled: true,
    //这里存储默认使用的是session
    // 可以选择哪些进入local存储，这样就不用全部都进去存储了
    // 默认是全部进去存储
    strategies: [{
      storage: localStorage,//localStorage存储多个key
      paths: ['name'],
      key: "name"
  }, {
      storage: localStorage,
      paths: ['age'],
      key: "age"
  },{
      storage: sessionStorage,//sessionStorage存储多个key
      paths: ['accessToken'],
      key: "accessToken"
  }],
  }
});
